package com.ruoyi.crop.controller;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.crop.api.domain.ClassAround;
import com.ruoyi.crop.api.domain.PlanAround;
import com.ruoyi.crop.api.domain.PlanClasses;
import com.ruoyi.crop.service.AroundTeamService;
import com.ruoyi.crop.service.ClassAroundService;
import com.ruoyi.crop.service.PlanAroundService;
import com.ruoyi.crop.service.PlanClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("around")
public class AroundController extends BaseController {
    @Autowired
    ClassAroundService classAroundService;
    @Autowired
    PlanAroundService planAroundService;
    @Autowired
    PlanClassesService planClassesService;
    @Autowired
    AroundTeamService aroundTeamService;
    //获取轮班列表分页
    @RequestMapping("AroundListPage")
    public TableDataInfo AroundListPage(Integer planId,Integer aroundId) throws ParseException {
        startPage();
        List<ClassAround> list=classAroundService.lists(planId,aroundId);
        return getDataTable(list);
    }



    /**
     * 新增班组
     */

    @PostMapping
    public AjaxResult add(@RequestBody ClassAround classAround)
    {
        if (classAround.getAroundType()!=null && classAround.getAroundType()==1){
            if (classAround.getAroundSize() >=2){
                return AjaxResult.error("轮班类型为两班倒时，轮班数量必须为2");
            }
        }else if (classAround.getAroundType()!=null && classAround.getAroundType()==2){
            if (classAround.getAroundSize() >=3){
                return AjaxResult.error("轮班类型为三班倒时，轮班数量必须为3");
            }
        }else if (classAround.getAroundType()!=null && classAround.getAroundType()==0){
            System.out.println(classAround.getAroundSize()+"11111111");
            if (classAround.getAroundSize() >=1){
                return AjaxResult.error("轮班类型为白班时，轮班数量必须为1");
            }
        }
        String s = IdUtil.getSnowflake().nextIdStr();
        classAround.setId(s);
        Integer planId = classAround.getPlanId();
        PlanClasses planClasses = planClassesService.getById(planId);
        classAround.setAroundId(planClasses.getAroundId());
        classAround.setPlanId(planId);
        classAround.setCreateBy(SecurityUtils.getUsername());
        classAround.setCreateTime(new Date());
        boolean b=classAroundService.save(classAround);
        if (b){
            PlanAround planAround=new PlanAround();
            planAround.setAroundId(classAround.getId());
            planAround.setPlanId(classAround.getPlanId());
            planAroundService.save(planAround);
        }
        return b?AjaxResult.success():AjaxResult.error();
    }

    /**
     * 修改班组
     */
    @PutMapping
    public AjaxResult edit(@RequestBody  ClassAround classAround)
    {
        classAround.setUpdateBy(SecurityUtils.getUsername());
        classAround.setUpdateTime(new Date());
        return toAjax(classAroundService.updateById(classAround));
    }

    /**
     * 删除零售
     */
    @DeleteMapping("/{teamId}")
    public AjaxResult remove(@PathVariable String[] teamId)
    {
        System.err.println(teamId);
        for (String s : teamId) {
            classAroundService.deleteById(s);
        }
//        classAroundService.deleteBatchByIds(Arrays.asList(teamId));
        return success();
    }
}
